# 安装文件记录
# 库文件
# 头文件
# 其他文件
# 并且记录到 安装目录 install_files.txt
# 创建软件链接 到 CMAKE_INSTALL_PREFIX 指定目录下
function(create_install_files TARGET_PATH SYMLINK_PATH)
    get_filename_component(SYMLINK_DIR ${SYMLINK_PATH} DIRECTORY)
    # 使用 execute_process 先创建父目录（如果不存在）
    execute_process(
        COMMAND ${CMAKE_COMMAND} -E make_directory ${SYMLINK_DIR}
        RESULT_VARIABLE MKDIR_RESULT
        ERROR_VARIABLE MKDIR_ERROR
    )
    if(NOT MKDIR_RESULT EQUAL 0)
        message(FATAL_ERROR "父目录 ${SYMLINK_DIR} 创建失败: ${MKDIR_ERROR}")
    endif()
    # 创建或更新软链接（-sf：强制覆盖）
    execute_process(
        COMMAND ${CMAKE_COMMAND} -E create_symlink ${TARGET_PATH} ${SYMLINK_PATH}
        RESULT_VARIABLE LN_RESULT
        ERROR_VARIABLE LN_ERROR
    )
    if(NOT LN_RESULT EQUAL 0)
        message(FATAL_ERROR "软链接创建失败 ${SYMLINK_PATH} -> ${TARGET_PATH}: ${LN_ERROR}")
    endif()
endfunction()
cmake_policy(SET CMP0009 NEW)  # 明确关闭跟随符号链接
### 安装文件记录
### 库文件
file(GLOB_RECURSE LIB_FILES RELATIVE ${INSTALL_DIR}/lib ${INSTALL_DIR}/lib/*)
### 头文件
file(GLOB_RECURSE INC_FILES RELATIVE ${INSTALL_DIR}/include ${INSTALL_DIR}/include/*)
file(WRITE ${INSTALL_DIR}/install_files.txt "# 安装文件记录\n")
foreach(file ${LIB_FILES})
    create_install_files(${INSTALL_DIR}/lib/${file} ${CMAKE_PREFIX_PATH}/lib/${file})
    file(APPEND ${INSTALL_DIR}/install_files.txt "${INSTALL_DIR}/lib/${file}\n")
endforeach()
foreach(file ${INC_FILES})
    create_install_files(${INSTALL_DIR}/include/${file} ${CMAKE_PREFIX_PATH}/include/${file})
    file(APPEND ${INSTALL_DIR}/install_files.txt "${INSTALL_DIR}/include/${file}\n")
endforeach()
